const Boxes = require("../model/boxes.model.js");

class BoxesService {
  //保存接头盒信息，如果没有companyId则为新增，否则为更新
  async saveBoxes({
    ID,
    BoxID,
    BoxName,
    BoxCode,
    Runtime,
    linkId,
    cdate,
    Latitude,
    Longitude,
    Z
  }) {
    var update = false;
    var boxes = {};
    var whereOpt = {};
    if (ID && ID > 0) {
      update = true;
      whereOpt.ID = ID;
    }
    linkId > 0 && Object.assign(boxes, { linkId });
    BoxID && Object.assign(boxes, { BoxID });
    BoxName && Object.assign(boxes, { BoxName });
    BoxCode && Object.assign(boxes, { BoxCode });
    cdate && Object.assign(boxes, { cdate });
    Longitude && Object.assign(boxes, { Longitude });
    Latitude && Object.assign(boxes, { Latitude });
    Z && Object.assign(boxes, { Z });
    if (!update) {
      Runtime = new Date().getTime();
    }
    Runtime > 0 && Object.assign(boxes, { Runtime });
    if (Object.keys(boxes).length == 0) {
      return false;
    }
    var res;
    try {
      if (update) {
        res = await Boxes.update(boxes, { where: whereOpt });
        if (res[0] > 0) {
          boxes.BoxID = BoxID;
          return JSON.parse(JSON.stringify(boxes));;
        } else {
          return false;
        }
      } else {
        res = await Boxes.create(boxes);
        if (res.dataValues == undefined) {
          return false;
        } else {
          return JSON.parse(JSON.stringify(res.dataValues));
        }
      }
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  //删除boxes
  async eraseBoxesById(BoxID) {
    if (!BoxID || BoxID <= 0) {
      return false;
    }
    var res;
    try {
      res = Boxes.destroy({ where: { BoxID } });
      if (!res) {
        return false;
      }
      return true;
    } catch (error) {
      console.log(error);
      return null;
    }
  }
    /* 
  删除指定条件的接头盒
  */
 async eraseBoxes({linkId}){
  var whereOpt = {};
  linkId && Object.assign(whereOpt,{linkId});
  if(Object.keys(whereOpt).length == 0){
    return false;
  }
  var res;
  try {
    res = await Boxes.destroy({where:whereOpt});
    if(!res){
      return false;
    }else{
      return true;
    }
  } catch (error) {
    console.log(error);
    return null;
  }
 }
   /* 
  批量添加
  */
  async  batchAddBoxes(arr){
    if(!(arr instanceof Array) || arr.length == 0){
      return false;
    }
    var res;
    try {
      res = await Boxes.bulkCreate(arr,{
        fields:[
          "BoxID",
          "BoxName",
          "BoxCode",
          "Runtime",
          "cdate",
          "Longitude",
          "Latitude",
          "linkId",
          "Z"
        ]
      });
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  /*
   *
   *@page:分页选项
   *@page.pageIndex:当前第几页
   *@page.pageSize:每页多少条记录
   *@page.pageCount:总共多少页
   *@page.sum:总共多少条记录
   */
  async getBoxes({ BoxID, BoxName, BoxCode, linkId }) {
    var whereOpt = {};
    BoxID >= 0 && Object.assign(whereOpt, { BoxID });
    BoxName && Object.assign(whereOpt, { BoxName });
    BoxCode && Object.assign(whereOpt, { BoxCode });
    linkId && Object.assign(whereOpt, { linkId });
    var res;
    try {
      res = await Boxes.findAll({
        where: whereOpt,
        order: [["BoxID", "ASC"]],
      });
      if(!res || res == null){
        return [];
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
  async getBoxByID(ID) {
    if(!ID || ID == undefined || ID <= 0){
      return false;
    }
    var whereOpt = {};
    whereOpt.ID = ID;
    var res;
    try {
      res = await Boxes.findOne({
        where: whereOpt
      });
      if(!res || res == null){
        return {}
      }
      return JSON.parse(JSON.stringify(res));
    } catch (error) {
      console.log(error);
      return null;
    }
  }
}

module.exports = new BoxesService();
